package com.tfswue.ershoujiaoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tfswue.ershoujiaoyi.entity.User;
import com.tfswue.ershoujiaoyi.mapper.UserMapper;
import com.tfswue.ershoujiaoyi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User register(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    public User login(String username, String password) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username", username).eq("password", password);
        return userMapper.selectOne(query);
    }

    @Override
    public List<User> list() {
        return userMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public User getById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public User update(User user) {
        userMapper.updateById(user);
        return userMapper.selectById(user.getId());
    }

    @Override
    public boolean delete(Long id) {
        return userMapper.deleteById(id) > 0;
    }
}
